package com.yc.supermarket.web.controller;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

import com.yc.supermarket.entity.Admin;
import com.yc.supermarket.entity.User;
import com.yc.supermarket.service.IUserService;
import com.yc.supermarket.service.impl.UserServiceImpl;
import javax.servlet.http.HttpSession;

@WebServlet("/user")
public class UserController extends BaseController {
    private IUserService service = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op = req.getParameter("op");
        if ("login".equals(op)) {
            doLogin(req, resp);
        }else if("register".equals(op)){
            doRegister(req,resp);
        }else if("admlogin".equals(op)){
            doAdmLogin(req,resp);
        }else if("usermodify".equals(op)){
            doUsermodify(req,resp);
        }else if("find".equals(op)){
            doFind(req,resp);
        }else {
            resp.getWriter().print("WeiGe: no such Methond ...");
        }
    }
    protected void doFind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> map = parseRequest(req);
        System.out.println(map);
        Map<String, Object> m = service.find(map);
        System.out.println(service.find(map));
        printToJson(resp, m);
    }
    protected void doRegister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user=parseRequest(req,User.class);
        String uname=req.getParameter("uname");
        String upwd=req.getParameter("upwd");
        String usafe = req.getParameter("usafe");
        int t=doPics(req,resp,usafe);
        if(t==1){
            user.setUserName(uname);
            user.setUserPassword(upwd);
            int a=service.addUser(user);
            System.out.println("hte :"+a);
            printToJson(resp,a);
        }else{
            printToJson(resp,0);
        }
    }


    protected void doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user=parseRequest(req,User.class);
        String uname=req.getParameter("uname");
        String upwd=req.getParameter("upwd");
        String usafe = req.getParameter("usafe");
        int t=doPics(req,resp,usafe);
        if(t==1){
        user.setUserName(uname);
        user.setUserPassword(upwd);
        int a=service.login(user);
        System.out.println("hte :"+a);
        printToJson(resp,a);
        }else{
            printToJson(resp,0);
        }
    }

    protected void doAdmLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Admin admin=parseRequest(req,Admin.class);
        String aname=req.getParameter("aname");
        String apwd=req.getParameter("apwd");
        String usafe = req.getParameter("usafe");
        int t=doPics(req,resp,usafe);
        if(t==1){
            admin.setAdminName(aname);
            admin.setAdminPassword(apwd);
            int a=service.admlogin(admin);
            System.out.println("hte :"+a);
            printToJson(resp,a);
        }else{
            printToJson(resp,0);
        }
    }


    protected int doPics(HttpServletRequest req,HttpServletResponse resp,String usafe) throws IOException{
        HttpSession session=req.getSession();
        String scpat = (String) session.getAttribute("verityCode");
        System.out.println("who:"+usafe+","+scpat);
        if(scpat.equals(usafe)){
           return 1;
        }else{
           return 0;
        }
    }
    protected void doUsermodify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user=parseRequest(req,User.class);
        String upwd=req.getParameter("pass");
        String userName = req.getParameter("userName");
        String utel=req.getParameter("tele");
        String uaddr=req.getParameter("addr");
        user.setUserName(userName);
        user.setUserPassword(upwd);
        user.setUserAddr(uaddr);
        user.setUsertel(utel);
        int a=service.usermodify(user);
        System.out.println("hte :"+a);
        printToJson(resp,a);
    }
}
